﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Gui.Controls
{
    public partial class MetalTopMenu : UserControl
    {
        public EventHandler OnMenuClickEvent;

        public static Color menuForeColor;
        public static Color dropDownMenuForeColor;
        public static Color dropDownMenuBackColor;
        public static Color imageMarginGradientBegin;
        public static Color imageMarginGradientMiddle;
        public static Color imageMarginGradientEnd;

        public static Color menuItemSelected;
        public static Color menuItemSelectedGradientBegin;
        public static Color menuItemSelectedGradientEnd;
        public static Color menuItemPressedGradientBegin;
        public static Color menuItemPressedGradientEnd;
        public static Color menuBorder;
        public static Color menuItemBorder;

        /// <summary> Our font object </summary>
        private System.Drawing.Text.PrivateFontCollection pfc;

        public MetalTopMenu()
        {
            InitializeComponent();
            Menu.RenderMode = ToolStripRenderMode.ManagerRenderMode;
            Menu.Renderer = new MyRenderer();

            // Set the value of the double-buffering style bits to true.
            this.DoubleBuffered = true;
            this.UpdateStyles();
        }

        public void SetFont(string fontName, int fontSize, System.Drawing.FontStyle fontStyle)
        {
            if (fontName == "Default")
            {
                return;
            }

            // Load font from file
            this.pfc = new System.Drawing.Text.PrivateFontCollection();

            this.pfc.AddFontFile(Metal.Config.Paths.ThemesFolder + Metal.Config.Settings.Instance.GetValue("LastSettings.Theme", "BlackMetal") + "\\" + fontName + ".ttf");
            
            // Apply font
            Menu.Font = new System.Drawing.Font(this.pfc.Families[0], fontSize, fontStyle);
        }

        /// <summary>
        /// Add drop down menu with sub items
        /// </summary>
        /// <param name="item">The item to add</param>
        public void AddMenu(ToolStripMenuItem item)
        {
            //item.Font = new Font("SegoeUI", 9);
            
            Menu.Items.Add(item);
            item.MouseEnter += new EventHandler(OnMouseEnter);

            // Subscribe to item childrens (drop down menus)
            foreach (ToolStripMenuItem subItem in item.DropDownItems)
            {
                subItem.Click += new EventHandler(this.OnMenuClick);

                foreach (ToolStripMenuItem subsubItem in subItem.DropDownItems)
                {
                    subsubItem.Click += new EventHandler(this.OnMenuClick);
                }
            }
        }
        private class MyRenderer : ToolStripProfessionalRenderer
        {
            public MyRenderer() : base(new MyColors()) { }

            protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
            {
                if (e.Item.IsOnDropDown)
                {
                    e.ToolStrip.BackColor = dropDownMenuBackColor;
                }
                else
                {
                    e.Item.BackColor = Color.Transparent;
                }
                base.OnRenderMenuItemBackground(e);
            }

            protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
            {
                if (e.Item.IsOnDropDown)
                {
                    e.TextColor = dropDownMenuForeColor;
                }
                else
                {
                    e.TextColor = menuForeColor;
                }

                base.OnRenderItemText(e);
            }
        }

        private class MyColors : ProfessionalColorTable
        {
            public override Color ImageMarginGradientBegin
            {
                get
                {
                    return imageMarginGradientBegin;
                }
            }

            public override Color ImageMarginGradientMiddle
            {
                get
                {
                    return imageMarginGradientMiddle;
                }
            }

            public override Color ImageMarginGradientEnd
            {
                get
                {
                    return imageMarginGradientEnd;
                }
            }

            public override Color MenuItemBorder
            {
                get
                {
                    return menuItemBorder;
                }
            }

            public override Color MenuBorder
            {
                get
                {
                    return menuBorder;
                }
            }

            public override Color ToolStripDropDownBackground
            {
                get
                {
                    return dropDownMenuBackColor;
                }
            }

            public override Color MenuItemPressedGradientBegin
            {
                get
                {
                    return menuItemPressedGradientBegin;
                }
            }

            public override Color MenuItemPressedGradientEnd
            {
                get
                {
                    return menuItemPressedGradientEnd;
                }
            }

            public override Color MenuItemSelected
            {
                get
                {
                    return menuItemSelected;
                }
            }

            public override Color MenuItemSelectedGradientBegin
            {
                get
                {
                    return menuItemSelectedGradientBegin;
                }
            }

            public override Color MenuItemSelectedGradientEnd
            {
                get
                {
                    return menuItemSelectedGradientEnd;
                }
            }
        }

        public void AddSeparator()
        {
            //ToolStripSeparator separator = new ToolStripSeparator();
            //Menu.Items.Add(separator);
            
            //ToolStripMenuItem separator = new ToolStripMenuItem();
            //separator.Text = "-";
            //Menu.Items.Add(separator);
        }

        public Image MenuBackground
        {
            set
            {
                this.Menu.BackgroundImage = value;
            }
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
            this.ParentForm.Focus();
        }

        private void OnMenuClick(object sender, EventArgs e)
        {
            // If is null it means that the in "Command" attribute in the xml is specified a invalid (inexistent) method
            if (this.OnMenuClickEvent != null)
                this.OnMenuClickEvent(sender, e);
        }

        private void Menu_MouseDown(object sender, MouseEventArgs e)
        {
            UserControl parent = this.Parent as UserControl;
            if (parent != null)
                BaseForm.FormMove.Instance.Start(e, parent.ParentForm);
            else
                BaseForm.FormMove.Instance.Start(e, this.Parent as Form);
        }

        private void Menu_MouseMove(object sender, MouseEventArgs e)
        {
            UserControl parent = this.Parent as UserControl;
            if (parent != null) // The menu is in a user control
                BaseForm.FormMove.Instance.Move(e, parent.ParentForm);
            else // The menu is in a form
                BaseForm.FormMove.Instance.Move(e, this.Parent as Form);
        }

        private void Menu_MouseUp(object sender, MouseEventArgs e)
        {
            BaseForm.FormMove.Instance.Stop();
        }
    }
}
